package com.laviedesfons.jhmtasc.types.abstracts;

import com.laviedesfons.jhmtasc.exceptions.MException;
import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.Value;
import fr.x9c.cadmium.primitives.cadmium.Cadmium;

/**
 *
 * @author Romano
 */
public abstract class CustomType<T> extends AbstractType<T> {

    public CustomType() {
    }

    protected CustomType(T v){
        wrapped = v;
    }

    @Override
    public CustomType<T> fromValue(Value v) throws MException {
        if (v.isBlock()) {
            return (CustomType<T>) v.asBlock().asCustom();
        }
        throw new MException("value is not a custom block");
    }

    @Override
    public Value toValue() {
        return Value.createFromBlock(
                Block.createBlock(getParentId(), Cadmium.createObject(this)));
    }

}
